package com.springapi.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.springapi.entity.Goodsmanager;
import com.springapi.entity.Users;
import com.springapi.service.UsersService;
import com.springapi.utils.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * (Users)表控制层
 *
 * @author makejava
 * @since 2024-12-03 09:22:47
 */
@RestController
@RequestMapping("/api/users")
public class UsersController {
    /**
     * 服务对象
     */
    @Resource
    private UsersService usersService;

    @Autowired
    TokenUtil tokenUtil;


    //查询全部员工
    @GetMapping("/myfindAll")
    public PageInfo<Users> myfindAll(@RequestParam int appid, @RequestParam int shopid, @RequestParam int page, @RequestParam int size, @RequestParam int rolesid) {

        System.out.println("appid:"+appid);
        System.out.println("shopid:"+shopid);
        System.out.println("rolesid:"+rolesid);
        return this.usersService.myfindAll(appid,shopid,page,size,rolesid);
    }


    //查询全部员工
    @GetMapping("/checktoken")
    public Boolean checktoken(@RequestParam String mytoken) {

        return tokenUtil.validateToken(mytoken);
    }

    @GetMapping("/finduserbyphone")
    public ResponseEntity<Users> finduserbyphone (@RequestParam int appid, @RequestParam int shopid, @RequestParam String phone) {
        return ResponseEntity.ok(this.usersService.findUserByUserphone(appid,shopid,phone));
    }

    @GetMapping("/findUserByUsername")
    public Map<String, Object> findUserByUsername(@RequestParam String username, @RequestParam String password) {
        Map<String, Object>  response  = new HashMap<>();
        Users user = null;
        try {
            user = usersService.findUserByUsername(username, password);
            if (user == null) {
                response.put("user","空");
                response.put("token","空");

            }else {
                //生成token，传到前台就可以了
                String token = tokenUtil.generateToken();

                response.put("user",user);
                response.put("token",token);
            }

        } catch (Exception e) {
            response.put("user","空");
            response.put("token","空");
            System.out.println(e.getMessage());
        }
        return response;
    }


    /**
     * 分页查询
     *
     * @param users 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @GetMapping
    public ResponseEntity<Page<Users>> queryByPage(Users users, PageRequest pageRequest) {
        return ResponseEntity.ok(this.usersService.queryByPage(users, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Users> queryById(@PathVariable("id") Integer id) {
        System.out.println("前台传入id："+id);
        return ResponseEntity.ok(this.usersService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param users 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<Users> add(@RequestBody Users users) throws JsonProcessingException {
        //根据前台传过来的手机号查找是否存在该用户
        Users user1 = this.usersService.findUserByUserphone(users.getAppid(),users.getShopid(),users.getPhone());
        Users user2=new Users();
        ObjectMapper mapper0 = new ObjectMapper();
        String jsonString0 = mapper0.writeValueAsString(user1);
        System.out.println("=================="+jsonString0);
        if(user1 == null ){

            user2=this.usersService.insert(users);
        }else {
            user2.setName("用户已存在");
        }


        return ResponseEntity.ok(user2);
    }

    /**
     * 编辑数据
     *
     * @param users 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<Users> edit(@RequestBody Users users) {
        return ResponseEntity.ok(this.usersService.update(users));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Integer id) {
        return ResponseEntity.ok(this.usersService.deleteById(id));
    }

}

